<template>
  <view>
    <uni-popup ref="joinOrCreateCompany" class="joinOrCreateCompany">
      <uni-popup-dialog
          type="info"
          cancelText="取消"
          :confirmText="type == 1 ? '创建企业' : '搜索并加入企业'"
          :title="type == 1 ? '创建企业' : '加入企业'"
          @close="cancel"
          @confirm="submitForm"
          before-close>
        <template v-slot="content">
          <uni-notice-bar
              color="#00a660"
              background-color="#e6f6ef"
              style="margin-bottom: 60rpx;"
              :text="type == 1?'创建企业/单位/组织，完成实名认证，您将自动成为该账户管理员，拥有账户全部权限！':'加入企业/单位/组织，申请信息提交后，请等待该账户管理员审核，通过审核即可加入！'"
          />
          <uni-forms ref="form" :modelValue="form" :rules="rules">
            <uni-forms-item name="companyName" required>
              <span style="color: #606266;font-size: 24rpx;">请输入您的企业名称，务必与营业执照上的名称保持一致</span>
              <uni-easyinput
                  style="margin-top: 10rpx;"
                  v-model="form.companyName"
                  :maxlength="50"
                  placeholder="请输入企业名称"/>
            </uni-forms-item>
          </uni-forms>
        </template>
      </uni-popup-dialog>
    </uni-popup>
	<!-- 企业实名认证弹出框 -->
	<company-real-name ref="companyAuth"></company-real-name>
  </view>
</template>

<script>
import companyRealName from "@/components/identityPoup/companyRealName"
import {createCompany, joinCompany} from "@/api/ec/common/main";
import {updateSession} from "@/api/ec/person/user/userInfo";
import store from "@/store"

export default {
  components: {companyRealName},
  name: "joinOrCreateCompany",
  data() {
    return {
      // 类型 1 创建企业 ， 2 加入企业
      type: 1,
      // 表单
      form: {},
      // 校验
      rules: {
        companyName: {
          rules: [
            {required: true, errorMessage: '请输入企业名称'},
            {minLength: 2, maxLength: 50, errorMessage: '企业名称长度需要在2-50个汉字或字符之间，不能包含特殊字符'},
          ]
        },
      },
    };
  },
  methods: {
    /** 打开弹出框 */
    toOpenDialog(type) {
      this.type = type;
      this.$refs.joinOrCreateCompany.open('center')
      this.form = {};
    },
    /** 取消按钮 */
    cancel() {
      this.$refs.joinOrCreateCompany.close()
    },
    /** 1 创建企业 ， 2 加入企业 */
    submitForm() {
      this.$refs.form.validate().then(valid => {
        if (valid) {
          // 创建企业
          if (this.type == 1) {
            this.toCreateCompany();
          } else {
            this.toJoinCompany();
          }
        }
      });
    },
    /** 创建企业 */
    toCreateCompany() {
      let companyName = this.form.companyName;
      const param = {companyName};
      uni.showLoading({title: '加载中...', mask: true});
      createCompany(param).then(response => {
        uni.hideLoading();
        this.cancel();
        let company = response.data;
        // 刷新登录用户session信息
        updateSession().then((response) => {
          store.commit('SET_USER', response.data);
          this.$emit("success", "");
        });
        // 弹框提示
		setTimeout(()=>{
			this.$refs.companyAuth.open(company.id)
		},300)
        // uni.showModal({
        //   title: '提示：',
        //   cancelText: '取消',
        //   confirmColor: '#00a660',
        //   confirmText: '立即认证',
        //   content: '企业创建成功，请进行实名认证！',
        //   success: function (res) {
        //     if (res.confirm) {
        //       uni.navigateTo({url: '/pages/authentication/companyRealname/start?id=' + company.id});
        //     }
        //   }
        // })
      }).catch(() => {
        uni.hideLoading();
      });
    },
    /** 搜索并加入 */
    toJoinCompany() {
      let companyName = this.form.companyName;
      const param = {companyName};
      uni.showLoading({title: '加载中...', mask: true});
      joinCompany(param).then(() => {
        uni.hideLoading();
        this.cancel();
        this.$modal.msgSuccess("您申请加入【" + companyName + "】信息已提交，待管理员审核通过后，即可加入！");
      }).catch(() => {
        uni.hideLoading();
      });
    },
  },
}
</script>

<style scoped lang="scss">

::v-deep .uni-forms-item .is-required {
  display: none;
}

::v-deep .uni-popup-dialog {
  width: 90%;
}

.joinOrCreateCompany ::v-deep .uni-dialog-content {
  padding: 26rpx;
  display: block;
}


</style>